package org.example.DAO.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.example.DAO.GroupInfoDAO;
import org.example.Entity.GroupInfo;
import org.example.Utill.DatabaseUtil;

public class GroupInfoDAOImpl implements GroupInfoDAO {
    // 添加小组
    @Override
    public boolean addGroup(GroupInfo group) {
        String sql = "INSERT INTO groups (group_id, group_name, class_id) VALUES (?, ?, ?)";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, Integer.parseInt(group.getGroupID()));
            pstmt.setString(2, group.getGroupName());
            pstmt.setInt(3, Integer.parseInt(group.getClassID()));
            return pstmt.executeUpdate() > 0;
            
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    @Override
    
    // 获取指定班级的所有小组
    public List<GroupInfo> getGroupsByClass(String classId) {
        List<GroupInfo> groups = new ArrayList<>();
        String sql = "SELECT * FROM groups WHERE class_id = ?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, classId);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                GroupInfo group = new GroupInfo(
                    rs.getString("group_name"),
                    rs.getString("group_id"),
                    rs.getString("class_id")
                );
                groups.add(group);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return groups;
    }
    @Override

    public List<GroupInfo> getGroupsByClassId(String classId) {
        List<GroupInfo> groups = new ArrayList<>();
        String sql = "SELECT * FROM groups WHERE class_id = ?";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, classId);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                GroupInfo group = new GroupInfo(
                    rs.getString("group_name"),
                    rs.getString("group_id"),
                    rs.getString("class_id")
                );
                groups.add(group);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return groups;
    }
    @Override
    public List<GroupInfo> getGroupIdByName(String groupName, String classId){
        List<GroupInfo> groups = new ArrayList<>();
        String sql = "SELECT * FROM groups WHERE group_name = ? AND class_id = ?";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, groupName);
            pstmt.setString(2, classId);
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                GroupInfo group = new GroupInfo(
                    rs.getString("group_name"),
                    rs.getString("group_id"),
                    rs.getString("class_id")
                );
                groups.add(group);

            }
             } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return groups;
    }
    @Override
    public boolean updateGroupName(String currentGroupName, String trim, String currentClassName) {
        String sql = "UPDATE groups SET group_name = ? WHERE group_name = ? AND class_id = ?";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, trim);
            pstmt.setString(2, currentGroupName);
            pstmt.setString(3, currentClassName);
            return pstmt.executeUpdate() > 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    @Override
    public boolean deleteGroupByName(String currentGroupName, String currentClassName) {
        String sql = "DELETE FROM groups WHERE group_name = ? AND class_id = ?";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, currentGroupName);
            pstmt.setString(2, currentClassName);
            return pstmt.executeUpdate() > 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    @Override

    public boolean addGroupInfo(GroupInfo newGroup) {
        String sql = "INSERT INTO groups (group_id, group_name, class_id) VALUES (?, ?, ?)";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, Integer.parseInt(newGroup.getGroupID()));
            pstmt.setString(2, newGroup.getGroupName());
            pstmt.setInt(3, Integer.parseInt(newGroup.getClassID()));
            return pstmt.executeUpdate() > 0;

        } catch (SQLException e) {}
        return false;
    }
}
